package net.haier.fashion.trend.report.core.filter;

import net.haier.fashion.trend.report.util.ThreadLocalUtil;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class TraceIdFilter extends OncePerRequestFilter {
    public TraceIdFilter() {
    }

    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        ThreadLocalUtil.getAndSetTraceId();
        try {
            filterChain.doFilter(request, response);
        } finally {
            ThreadLocalUtil.clearTraceId();
        }

    }
}
